#!/opt/perl/bin/perl -w
#########################################################################
#                                                                       #
# Name:         check_env_v_prod.pl (Perl script)                       #
#                                                                       #
# Completed:    30th May, 2006.                                         #
#                                                                       #
# Updated:	8th June 2006.                                          #
#                                                                       #
# Author:       Nev Lawrence, Proprius Consulting Ltd.                  #
#                                                                       #
# Arguments:    project_name                                            #
#                                                                       #
# Purpose:	A script to compare a given environment's code against	#
#		production. The idea is to run this script from cron	#
#		on a periodic basis to give users in the given env an	#
#		indication of what production updates have happened.	#
#                                                                       #
#########################################################################

use warnings;
use strict;
use Data::Dumper;
use File::Basename;
use POSIX 'strftime';

# Set constants
my $prg=basename($0);
my $devenv=undef;
my $date_strng=strftime("%Y%m%d",localtime);
my $cm2_tag_out="/tmp/$prg.cm2_tag_out";
my $cm2_cmp_out="/tmp/$prg.cm2_cmp_out";
my $cm2_dff_out="/tmp/$prg.cm2_dff_out";
my $mailid='neville.lawrence@bankofamerica.com neville.lawrence@bankofamerica.com';

sub usage {
        print "\nUsage: $prg project_name

$prg compares the given environment's (project_name) code 
against production by using the \"cm2 compare projcets\" utility. A new
\"cm2 tag\" is created in the given environment first.

Written by Nev Lawrence 2006, proprius.co.uk
";
        exit 1;
}

# Have we been passed a project name?
if (! defined($ARGV[0])) {
        print "Project Name not specified\n";
        usage;
}
$devenv = $ARGV[0];

# Can we cd to the given project name?
system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv");
my $rc=($?/256);
if ($rc != 0) {
	print "Unable to change to project directory for env $devenv\n";
	exit 2;
}

# Create a new tag in the given env first
system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv; /sas/rcs/bin/cm2 tag release --tag $date_strng --desc \"For comparison purposes\" --verbose --allowopen > $cm2_tag_out 2>&1");
$rc=($?/256);
if ($rc == 0) {
	system("mailx -s\"Aug Rel: Weekly code comparison - Creation of tag in $devenv\" $mailid < $cm2_tag_out");
} else{
	system("mailx -s\"Aug Rel: Weekly code comparison - Creation of tag in $devenv failed\" $mailid < $cm2_tag_out");
	exit 2;
}

# Compare code in given env against Production
system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv; /sas/rcs/bin/cmpprojects --projects=production,$devenv --action=report 2>&1 | grep -v \"^  metainfo\" > $cm2_cmp_out 2>&1");
$rc=($?/256);
if ($rc == 0) {
	# List what's currently checked out as well
	system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv; /sas/rcs/bin/cm2 status --level=project 2>&1 | grep -v \"rlog error\" >> $cm2_cmp_out 2>&1");

	# Do a "merge preview" to identify if any changes would come from production
	system("echo \"\nPreview merge (any changes coming from production?):\" >> $cm2_cmp_out");
	system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv; /sas/rcs/bin/cmpprojects --projects=production,$devenv --preview --action=merge --mergetarget=+$devenv 2>&1 | grep -vE \"metainfo|rlog error\" >> $cm2_cmp_out 2>&1");

	# List the actual diff's between "files in both"
	system(". /etc/MBNA_env/504-codemgr2.env; cm2cd $devenv; /sas/rcs/bin/cmpprojects --projects=production,$devenv --action=diff 2>&1 | grep -v \"^  metainfo\" > $cm2_dff_out 2>&1");

	system("mailx -s\"Aug Rel: Weekly code comparison - Code compare of $devenv against production\" $mailid < $cm2_cmp_out");
	system("mailx -s\"Aug Rel: Weekly code comparison - Code diffs of $devenv against production\" $mailid < $cm2_dff_out");
} else{
	system("mailx -s\"Aug Rel: Weekly code comparison - Code compare of $devenv against production failed\" $mailid < $cm2_cmp_out");
	exit 2;
}
